// pages/login/login.js

import {
    request
} from "../../request/request.js";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 账号密码。
        account: "",
        password: "",
        isLogin: false,//用来判断渲染登陆页面还是收藏页面
        questionId: [],
        question: [],
        showList: [],
        // 当前页数，从1开始
        pageNum: 1,
        // 后端返回的总页数
        totalPageNum: 0,
        // 当前是否是最后一页
        isBottom: false,
        // 触底事件防抖动
        Loading: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onShow(options) {
        console.log("onShow")
        var that = this
        this.setData({
            pageNum: 1,
            isBottom: false,
            isLogin: wx.getStorageSync('isLogin'),
            questionId: [],
            question: [],
            showList: []

        })
        // console.log(this.data.isLogin)
        // 如果已经登录，请求收藏问题列表
        if (this.data.isLogin) {
            console.log("已登录")
            this.getQuestionId()
            setTimeout(() => {
                this.getQuestionList()
            }, 100);
        }
    },
// 绑定账号input框
    bindAccount(e) {
        console.log("account:" + e.detail.value)
        this.setData({
            account: e.detail.value.replace(/\s+/g, '')
        })
    },
    bindPwd(e) {
        console.log("Pwd:" + e.detail.value)
        this.setData({
            password: e.detail.value.replace(/\s+/g, '')//用''代替空格
        })
    },
    login() {
        if (this.data.account == '') {
            wx.showToast({
                title: '账号不能为空',
                icon: "none"
            })
            return
        }
        if (this.data.password == '') {
            wx.showToast({
                title: '密码不能为空',
                icon: "none"
            })
            return
        }
        request({
                url: "/user/loginByPwd",
                method: "POST",
                data: {
                    "account": this.data.account,
                    "password": this.data.password
                }
            })
            .then(res => {
                if (res.data.code == 200) {
                    wx.setStorageSync('isLogin', true)
                    wx.setStorageSync('token', res.data.data.token)
                    wx.setStorageSync('userId', res.data.data.userId)
                    wx.setStorageSync('roleId', res.data.data.roleId)
                    wx.switchTab({
                        url: "/pages/index/index"
                    });
                    console.log("页面跳转主页");
                    wx.showToast({
                        title: '登录成功',
                        icon: "success",
                    })
                }
                if (res.data.code == 400) {
                    console.log("页面跳转主页");
                    wx.showToast({
                        title: res.data.msg,
                        icon: "error",
                    })
                }
            })
    },
    // 获取收藏题目的id列表
    getQuestionId() {
        request({
            url: "/collection/listCollection",
            header: {
                "Authorization": wx.getStorageSync('token')
            },
            method: "POST",
            data: {
                "ids": [wx.getStorageSync('userId')]
            }
        }).then(res => {
            console.log("收藏题目id：")
            console.log(res.data.data)
            res.data.data.forEach(item => {
                console.log('timuID::' + item.questionId)
                this.data.questionId.push(item.questionId)
            })
            this.setData({
                questionId: this.data.questionId
            })
        })
    },
    // 使用id数组获取题目
    getQuestionList() {
        if (this.data.questionId.length!=0) {
            request({
                url: "/question/listQuestion",
                header: {
                    "Authorization": wx.getStorageSync('token')
                },
                method: "POST",
                data: {
                    "ids": this.data.questionId
                }
            }).then(res => {
                console.log("题目列表：")
                console.log(res)
                this.setData({
                    question: res.data.data
                })
                res.data.data.forEach(item => {
                    var str = item.title.split("<picture>")
                    this.data.showList.push(str)
                    this.setData({
                        showList: this.data.showList,
                    });
                })
            })
        }
    },
    // 触底翻页
    scrollReachBottom(e) {
        console.log("触底")
        console.log(e)
        // 页数++
        this.data.pageNum++
        //当前页数大于总页数
        if (this.data.pageNum > this.data.totalPageNum) {
            this.setData({
                isBottom: true
            })
        }
        //已经加载完毕
        if (this.data.isBottom) {
            wx.showToast({
                title: '已加载完全部题目',
                icon: "none"
            })
            return
        }
        //没加载完全部题目
        else {
            console.log("还没加载完")
            if (this.data.Loading) {
                return
            }
            this.setData({
                Loading: true
            })
            wx.showLoading({
                title: '加载中',
            })
            // 发送下一页请求
            request({
                url: "/question/pageQuestionCollection?page=" + this.data.pageNum + "&pageSize=5",
                header: {
                    "Authorization": wx.getStorageSync('token')
                },
                method: 'POST',
                data: {
                    "subjectId": this.data.subjectId,
                    "chapterId": this.data.chapterId,
                    "sectionId": this.data.sectionId
                }
            }).then(res => {
                wx.hideLoading()
                console.log("题目查询：")
                console.log(res.data.data.records)

                this.setData({
                    Loading: false,
                    questionList: [...this.data.questionList, ...res.data.data.records],
                })
                setTimeout(() => {
                    this.renderLatex()
                }, 500);
            })
        }
    }
})